package com.jtech.together.view.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jtech.together.R;
import com.jtech.together.model.CommentModel;
import com.jtech.together.model.TrendsModel;
import com.jtech.together.model.UserModel;
import com.jtech.together.model.event.TrendsEvent;
import com.jtech.together.util.ImageUtils;
import com.jtechlib2.adapter.RecyclerAdapter;
import com.jtechlib2.view.RecyclerHolder;

import java.util.Arrays;

/**
 * 评论列表适配器
 * Created by wuxubaiyang on 2017/5/16.
 */

public class CommentAdapter extends RecyclerAdapter<CommentModel> {
    private static final int TYPE_TRENDS_DETAIL = 0x4516;

    private OnCommentListener onCommentListener;
    private TrendsAdapter trendsAdapter;

    public CommentAdapter(Activity context) {
        super(context);
        //实例化动态列表的适配器
        this.trendsAdapter = new TrendsAdapter(context);
    }

    /**
     * 设置评论监听
     *
     * @param onCommentListener
     */
    public void setOnCommentListener(OnCommentListener onCommentListener) {
        this.onCommentListener = onCommentListener;
    }

    /**
     * 设置动态监听
     *
     * @param onTrendsListener
     */
    public void setOnTrendsListener(TrendsAdapter.OnTrendsListener onTrendsListener) {
        trendsAdapter.setOnTrendsListener(onTrendsListener);
    }

    /**
     * 添加一条评论
     *
     * @param model
     */
    public void addComment(CommentModel model) {
        getRealDatas().add(0, model);
        notifyDataSetChanged();
    }

    /**
     * 获取动态对象
     *
     * @return
     */
    public TrendsModel getTrendsModel() {
        if (trendsAdapter.getItemCount() > 0) {
            return trendsAdapter.getItem(0);
        }
        return null;
    }

    /**
     * 更新动态
     *
     * @param event
     */
    public void updateTrends(TrendsEvent event) {
        String trendsId = event.getTrendsId();
        int count = event.getCount();
        switch (event.getState()) {
            case TrendsEvent.STATE_LIKE://喜欢
                trendsAdapter.updateItemLike(trendsId, count);
                break;
            case TrendsEvent.STATE_UNLIKE://踩
                trendsAdapter.updateItemUnlike(trendsId, count);
                break;
            case TrendsEvent.STATE_COMMENT://评论
                trendsAdapter.updateItemComment(trendsId, count);
                break;
        }
        notifyItemChanged(0);
    }

    @Override
    public int getItemCount() {
        return super.getItemCount() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TRENDS_DETAIL;
        }
        return super.getItemViewType(position);
    }

    /**
     * 设置动态
     *
     * @param trendsModel
     */
    public void setTrendsDetail(TrendsModel trendsModel) {
        trendsAdapter.setDatas(Arrays.asList(trendsModel));
        notifyItemChanged(0);
    }

    @Override
    protected View createView(LayoutInflater layoutInflater, ViewGroup viewGroup, int viewType) {
        if (viewType == TYPE_TRENDS_DETAIL) {
            return trendsAdapter.createView(layoutInflater, viewGroup, viewType);
        }
        return layoutInflater.inflate(R.layout.view_trends_comment_item, viewGroup, false);
    }

    @Override
    protected void convert(RecyclerHolder holder, CommentModel model, int position) {
        int viewType = getItemViewType(position);
        if (viewType == TYPE_TRENDS_DETAIL) {
            if (trendsAdapter.getItemCount() > 0) {
                trendsAdapter.convert(holder, trendsAdapter.getItem(0), 0);
            }
        } else {
            model = getItem(position - 1);
            final UserModel userModel = model.getUser();
            //显示昵称
            holder.setText(R.id.textview_trends_comment_nickname, userModel.getNickname());
            //显示内容
            holder.setText(R.id.textview_trends_comment_content, model.getContent());
            //显示头像
            ImageUtils.showCircleImage(getContext(), userModel.getAvatar(), holder.getImageView(R.id.imagebutton_trends_comment_avatar),
                    R.drawable.ic_default_avatar, R.drawable.ic_default_avatar);
            //设置头像点击事件
            holder.setClickListener(R.id.imagebutton_trends_comment_avatar, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onCommentListener) {
                        onCommentListener.onAvatarClick(userModel.getObjectId());
                    }
                }
            });
        }
    }

    /**
     * 评论列表监听
     */
    public interface OnCommentListener {
        void onAvatarClick(String userId);
    }
}